package com.example.oracle.system.exception;

import com.example.oracle.pojo.system.ResponseMap;
import com.example.oracle.pojo.system.WebCode;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * @author Siaze
 * @date 2021/11/16
 */
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    /**
     * 捕获系统请求所有异常
     * @param response reponse返回对象
     * @param ex 发送的异常
     * @return 返回JSON格式的异常信息
     */
    @ExceptionHandler(Exception.class)
    public ResponseMap globalException(HttpServletResponse response, Exception ex){
        ResponseMap result = new ResponseMap();
        result.setCode(WebCode.SYSTEM_ERROR.getCode());
        result.setMsg(ex.getMessage());
        return result;
    }

}
